package com.foxzzz.temporary;

public class Solution124 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
    int max=Integer.MIN_VALUE;
    public int postorder(TreeNode root){
        if(root==null)
            return 0;
        int left=postorder(root.left);
        int right=postorder(root.right);
        int maxPath=left>right?left:right;
        int val=root.val;
        //记录可能出现的最大值
        if(max<left+right+val)
            max=left+right+val;
        int sum=val+maxPath;
        max=max>sum?max:sum;
        //抛弃子树
        if(sum<0)
            return 0;
        else {
            return sum;
        }
    }


    public int maxPathSum(TreeNode root) {

        postorder(root);
        return this.max;
    }
    public  void  test(){
        TreeNode treeNode=new TreeNode(-1);

        maxPathSum(treeNode);
    }

    public static void main(String[] args) {
        new Solution124().test();
    }
}
